/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.LoaiToaTauDAO;
import dao.TauDAO;
import dao.ToaTauDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dto.LoaiToaTau;
import dto.Tau;
import dto.ToaTau;
import java.sql.SQLException;
 
public class ToaTauController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, InstantiationException, SQLException, IllegalAccessException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            /* TODO output your page here. You may use following sample code. */
            if(session.getAttribute("USERNAME")!=null){
            String action = request.getParameter("action");
            
             
           
            List<LoaiToaTau> dslnd = LoaiToaTauDAO.LayDSLoaiToaTau();
            request.setAttribute("dslnd", dslnd);
             List<Tau> dstau_toatau = TauDAO.LayDanhSachTau();
             request.setAttribute("dstau_toatau", dstau_toatau);
            if(action == null){
                List<ToaTau> dsnd  = ToaTauDAO.LayDanhSachToaTau();
                request.setAttribute("dsnd", dsnd);
                request.getRequestDispatcher("toatau.jsp").forward(request, response);
            } else if(action.equals("insert")){
                request.setAttribute("view", false);
                session.setAttribute("save", 0);
                request.getRequestDispatcher("thongtintoatau.jsp").forward(request, response);
            } else if(action.equals("Save")){
                
                ToaTau nd;
                if((Integer)session.getAttribute("save") == 0){
                    nd = new ToaTau();
                    //nd.setMatau(Integer.parseInt(request.getParameter("txtmatau")));
                }else{
                    nd = ToaTauDAO.LayThongTinToaTau(request.getParameter("txtmatoatau"));
                }
                nd.setStttoa(Integer.parseInt(request.getParameter("txtstttoa")));
                nd.setMatau(Integer.parseInt(request.getParameter("txtmatau")));
                nd.setSoluongghe(Integer.parseInt(request.getParameter("txtsoluongghe"))); 
                int trangthai = Integer.parseInt(request.getParameter("rdtrangthai"));
                nd.setTrangthai(trangthai);
                int maloaitau = Integer.parseInt(request.getParameter("cmdloaitoatau"));
                nd.setMaloaitoatau(maloaitau); 
                if((Integer)session.getAttribute("save") == 0){
                    ToaTauDAO.ThemToaTau(nd);
                } else {
                    ToaTauDAO.CapNhatToaTau(nd);
                }
                List<ToaTau> dsnd = ToaTauDAO.LayDanhSachToaTau();
                request.setAttribute("dsnd", dsnd);
                request.getRequestDispatcher("toatau.jsp").forward(request, response);
            } else if(action.equals("view")){
                request.setAttribute("view", true);
                ToaTau nd = ToaTauDAO.LayThongTinToaTau(request.getParameter("id"));
                 request.setAttribute("tauedit", nd);
                
                request.getRequestDispatcher("thongtintoatau.jsp").forward(request, response);
                
            } else if(action.equals("edit")){
                request.setAttribute("view", false);
                session.setAttribute("save", 1);
                ToaTau nd =ToaTauDAO.LayThongTinToaTau(request.getParameter("id"));
                request.setAttribute("tauedit", nd);
                request.getRequestDispatcher("thongtintoatau.jsp").forward(request, response);
            } else if(action.equals("Delete")){
                String[] checks = request.getParameterValues("delete");
                
                if(checks!=null){
                    for(int i=0; i < checks.length; i++){
                        ToaTauDAO.XoaToaTau(checks[i]);
                    }
                }
                
                List<ToaTau> dsnd = ToaTauDAO.LayDanhSachToaTau();
                request.setAttribute("dsnd", dsnd);
                request.getRequestDispatcher("toatau.jsp").forward(request, response);
              
            }
            } else {
                response.sendRedirect("Login");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ToaTauController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
